package com.unhz.taigu.interface_controllers.register;

import com.unhz.generator.dao.ProductionOrderInfoMapper;
import com.unhz.generator.enums.EnumProductionOrder;
import com.unhz.generator.enums.EnumValid;
import com.unhz.generator.pojo.Contract;
import com.unhz.generator.pojo.Order;
import com.unhz.generator.pojo.ProductionOrder;
import com.unhz.generator.pojo.ProductionOrderInfo;
import com.unhz.generator.utils.EmptyUtil;
import com.unhz.generator.utils.JsonResult;
import com.unhz.taigu.service.ContractService;
import com.unhz.taigu.service.OrderService;
import com.unhz.taigu.service.ProductionOrderInfoService;
import com.unhz.taigu.service.ProductionOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author: 沈佳庆
 * @Description: 排产订单
 * @Date: Created in 上午 11:04 2018/3/3 0003
 * @Modified By: V1.0.0
 */
@RestController
@RequestMapping("/IProductionOrder")
public class IProductionOrderController{

    @Autowired
    private ContractService contractService;

    @Autowired
    private ProductionOrderService productionOrderService;

    @Autowired
    private ProductionOrderInfoService productionOrderInfoService;

    @Autowired
    private OrderService orderService;

    /**
     * 新增排产订单前查询排产订单剩余数量
     * @param model
     * @param orderId
     * @return
     */
    @RequestMapping("/selectByInsert")
    public JsonResult selectProductionOrderByInsert(Model model, Integer orderId){
        //根据订单id查询合同中的尺码和数量
        System.out.println("-------------------"+orderId);
        List<Contract> contractList = contractService.findContractsByOrderId(orderId);
        //根据订单id查询排产订单中的数量
        List<ProductionOrder> productionOrderList = productionOrderService.selectByOrderId(Long.valueOf(orderId));
        int [] number = new int[contractList.size()];
        for(int i=0;i<productionOrderList.size();i++){
            Long productionId = Long.valueOf(productionOrderList.get(i).getId());
            List<ProductionOrderInfo> productionOrderInfoList =  productionOrderInfoService.selectByOrderId(productionId);
            //排产订单信息数量
            int nums;
            for(int j=0;j<productionOrderInfoList.size();j++){
                nums = productionOrderInfoList.get(j).getNumber();
                number[j] += nums;
            }
        }
        //排产订单剩余数量
        int count;
        int [] counts = new int[contractList.size()];
        for(int x=0;x<number.length;x++){
            count = contractList.get(x).getNumber() - number[x];
            counts[x] = count;
        }
        model.addAttribute("counts",counts);
        return JsonResult.success("查询排产订单剩余数量成功",counts);
    }


    /*   *//**
     * 增加排产订单
     * @param orderId 订单id
     * @param planTime 计划交期时间
     * @param number 数量
     * @param size 尺码
     * @return
     * @throws ParseException
     */
    @RequestMapping("/insert")
    public JsonResult insert(@RequestParam(value= "orderId",required = false) Integer orderId,
                       @RequestParam(value= "planTime",required = false) String planTime,
                       @RequestParam(value= "number",required = false) Integer[] number,
                       @RequestParam(value= "size",required = false) String[] size) throws ParseException {
        System.out.println(orderId+"---"+planTime+"---"+size.length+"---"+size);
       // ProductionOrder productionOrders = productionOrderService.selectNewProductionOrder(Long.valueOf(orderId));
      ProductionOrder productionOrder = new ProductionOrder();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date planDate = sdf.parse(planTime);
        System.out.println("--------------------"+planDate);
        productionOrder.setApplyTime(new Date());
        productionOrder.setPlanTime(planDate);
        productionOrder.setRealityTime(new Date());
        productionOrder.setOrderId(orderId);
        productionOrder.setStatus((byte) EnumProductionOrder.WAIT_NOTARIZE.getIndex());
        Order order = orderService.selectOrderById(orderId);
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmm");
        String date = df.format(new Date());
        int num=(int)(Math.random()*900)+100;
        String productionNo = order.getOrderNo()+ "B1"+num;
    /*    String no = null;
        if(EmptyUtil.isEmpty(productionNo)){
            no = order.getOrderNo() + "B1";
        }else{
            no = productionNo.substring(productionNo.lastIndexOf("B")+1);
        }
        int Number = Integer.parseInt(no) + 1;*/
       // String productionNumber = productionNo.substring(0,productionNo.lastIndexOf("B")+1) + Number;
        productionOrder.setProductionNo(productionNo);
        int count = productionOrderService.insertSelective(productionOrder);
       // int count = productionOrderService.insertProductionOrder(productionOrder);
        if(count<1){
            return JsonResult.failure("增加排产订单失败!");
        }
        Integer productionId = productionOrder.getId();
        System.out.println("----------------------"+productionId);
        ProductionOrderInfo productionOrderInfo = null;
        for(int i=0;i<size.length;i++){
            productionOrderInfo = new ProductionOrderInfo();
            productionOrderInfo.setProductionId(Long.valueOf(productionId));
            productionOrderInfo.setSize(size[i]+"#");
            productionOrderInfo.setNumber(number[i]);
            productionOrderInfo.setValid((byte) EnumValid.ENABLED.getIndex());
            productionOrderInfoService.insert(productionOrderInfo);
        }
        int h=6-size.length;
        for(int i=0;i<h;i++)
        {
            productionOrderInfo = new ProductionOrderInfo();
            productionOrderInfo.setProductionId(Long.valueOf(productionId));
            productionOrderInfo.setSize("");
            productionOrderInfo.setNumber(0);
            productionOrderInfo.setValid((byte) EnumValid.ENABLED.getIndex());
            productionOrderInfoService.insert(productionOrderInfo);

        }

        return JsonResult.success("增加排产订单成功!");
    }

    /**
     * 修改排产订单前查询排产订单剩余数量
     * @param model
     * @param productionIds
     * @return
     */
    @RequestMapping("/selectByUpdate")
    public JsonResult selectProductionOrderByUpdate(Model model, @RequestParam(value= "productionId",required = false) Integer productionIds){
        ProductionOrder productionOrder =  productionOrderService.selectById(productionIds);
        Integer orderId = productionOrder.getOrderId();
        //根据订单id查询合同中的尺码和数量
        List<Contract> contractList = contractService.findContractsByOrderId(orderId);
        //根据订单id查询排产订单中的数量
        List<ProductionOrder> productionOrderList = productionOrderService.selectByOrderId(Long.valueOf(orderId));
        int [] number = new int[contractList.size()];
        for(int i=0;i<productionOrderList.size();i++){
            Long productionId = Long.valueOf(productionOrderList.get(i).getId());
            if(productionId==Long.valueOf(productionIds)){
                continue;
            }
            List<ProductionOrderInfo> productionOrderInfoList =  productionOrderInfoService.selectByOrderId(productionId);
            //排产订单信息数量
            int nums;
            for(int j=0;j<productionOrderInfoList.size();j++){
                nums = productionOrderInfoList.get(j).getNumber();
                number[j] += nums;
            }
        }
        //排产订单剩余数量
        int count;
        int [] counts = new int[contractList.size()];
        for(int x=0;x<number.length;x++){
            count = contractList.get(x).getNumber() - number[x];
            counts[x] = count;
        }
        model.addAttribute("counts",counts);
        return JsonResult.success("查询排产订单剩余数量成功",counts);
    }

    /**
     * 修改排产订单
     * @param planTime 计划交期时间
     * @param number 数量
     * @param size 尺码
     * @return
     * @throws ParseException
     */
    @RequestMapping("/update")
    @ResponseBody
    public JsonResult update(@RequestParam(value= "productionId",required = false) Integer productionId,
                             @RequestParam(value= "planTime",required = false) String planTime,
                             @RequestParam(value= "number",required = false) Integer[] number,
                             @RequestParam(value= "size",required = false) String[] size) throws ParseException {
        ProductionOrder productionOrder = new ProductionOrder();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date planDate = sdf.parse(planTime);
        productionOrder.setPlanTime(planDate);
        productionOrder.setId(productionId);
        int count = productionOrderService.updateProductionOrder(productionOrder);
        if(EmptyUtil.isEmpty(count)){
            return JsonResult.failure("修改排产订单失败!");
        }
        List<ProductionOrderInfo> p=productionOrderInfoService.selectByOrderId(productionId.longValue());
        int i;
        ProductionOrderInfo productionOrderInfo = null;
        for(i=0;i<size.length;i++){
            productionOrderInfo = new ProductionOrderInfo();
            productionOrderInfo.setId(p.get(i).getId());
            productionOrderInfo.setSize(size[i]+"#");
            productionOrderInfo.setNumber(number[i]);

            productionOrderInfoService.updateProductionInfo(productionOrderInfo);
        }

        return JsonResult.success("修改排产订单成功!");
    }

    /**
     * 逻辑删除排产订单
     * @param productionId
     * @return
     */
    @RequestMapping("/delete")
    public JsonResult deleteProductionOrder(@RequestParam(value= "productionId",required = false) Integer productionId){

        ProductionOrder p=productionOrderService.selectById(productionId);
       int count = productionOrderService.deleteProductionOrder(p.getId().longValue());
        productionOrderInfoService.deleteProductionInfo(p.getOrderId().longValue());
        if(EmptyUtil.isEmpty(count)){
            return JsonResult.failure("删除排产订单失败!");
        }
        return JsonResult.success("删除排产订单成功!");
    }



}
